package handler

import (
	"github.com/gin-gonic/gin"
	"net/http"
	"starry_sky/internal/config"
	"starry_sky/internal/core"
	"starry_sky/internal/service/accesslog-service"
	"starry_sky/internal/tools/logger"
)

func GetAccessLogFileList(c *gin.Context) {
	fileList, err := accesslog_service.GetAccessLogFileList()
	if logger.HasError(err, "打开文件夹异常") {
		c.JSON(http.StatusInternalServerError, core.Err.WithMsg("系统日志文件夹打开异常"))
	} else {
		c.JSON(http.StatusOK, core.OK.WithData(fileList))
	}
}

func DownloadAccessLogFile(c *gin.Context) {
	fileName := c.PostForm("fileName")
	if fileName == "" {
		c.JSON(http.StatusInternalServerError, core.Err.WithMsg("接受参数错误"))
		return
	}
	c.File(config.LogConfig.WebLogConfig.Path + "/" + fileName + ".log")
}
